<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Circle;
use app\common\model\Config;
use app\common\model\District;
use app\common\model\Industry;
use app\common\model\Shop;
use app\common\model\ShopAudit;
use app\common\model\ShopFlag;
use app\common\model\Store;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 商圈列表
     */
    public function get_circle_list(){
        $province_id = $this->request->param('province_id', 27833);
        $city_id = $this->request->param('city_id', 28141);
        $area_id = $this->request->param('area_id', '');
        $where = ['province_id' => $province_id, 'city_id' => $city_id];
        if($area_id){
            $where['area_id'] = $area_id;
        }
        $list = Circle::where($where)->order('weigh desc')->field('id,name,lat,lng')->select();
        foreach($list as &$item){
            $item['shop_count'] = Shop::alias('shop')
                ->join('User user', 'shop.user_id = user.id')
                ->join('UserShopAudit audit', 'shop.user_id = audit.user_id')
                ->whereNull('audit.delete_time')
                ->where('user.status', 'normal')
                ->where('audit.status', ShopAudit::STATUS_AUDIT_PASS)
                ->where('audit.expiration_time', '>', time())
                ->where('shop.circle_id', $item['id'])
                ->where('shop.is_show', Shop::IS_SHOW_YES)
                ->group('shop.id')
                ->count();
        }
        $this->success('请求成功', $list);
    }

    /**
     * 行业列表
     */
    public function get_industry_list(){
        $where = ['is_show' => Industry::IS_SHOW_YES];
        $list = Industry::where($where)->order('weigh desc')->field('id,name')->select();
        $this->success('请求成功', $list);
    }
    /**
     * 标签列表
     */
    public function get_flag_list(){
        $list = ShopFlag::order('weigh desc')->field('id,name')->select();
        $this->success('请求成功', $list);
    }
    /**
     * 分会列表
     */
    public function get_store_list(){
        $district_id = $this->request->param('district_id/d',0);
        $list = Store::order('weigh desc')->where('district_id',$district_id)->field('id,name')->select();
        $this->success('请求成功', $list);
    }
    /**
     * 获取协议
     */
    public function get_all_agreement(){
        // 用户注册隐私协议
        $data['agreement'] = Config::where('name', 'user_agreement')->value('value');
        $data['agreement'] = replace_editor_content_images_url($data['agreement']);
        // 店家认证免责声明
        $data['safe_agreement'] = Config::where('name', 'safe_agreement')->value('value');
        $data['safe_agreement'] = replace_editor_content_images_url($data['safe_agreement']);
        $this->success("获取成功", $data);
    }

    /**
     * 引导关注
     * 获取关注h5地址
     */
    public function get_h5()
    {
        $data['h5_url'] = Config::where('name', 'h5_url')->value('value');
        $this->success("获取成功", $data);
    }

    /**
     * 获取技术支持介绍
     */
    public function get_technical(){
        $data['technical'] = Config::where('name', 'technical')->value('value');
        $data['technical'] = replace_editor_content_images_url($data['technical']);
        $this->success("获取成功", $data);
    }

    /**
     * 地区接口
     *
     * @return void
     */
    public function district()
    {
        $type = $this->request->param('type', District::TYPE_SHOP);
        $id = $this->request->get('id', 1);
        $row = District::getArea($id, $type);
        foreach($row as &$item){
            if($type == District::TYPE_SHOP){
                $item['name'] = $item['shop_name'];
            }
            unset($item['shop_name']);
        }
        $this->success('返回成功', $row);
    }

    /**
     * 地区接口
     * 只显示深圳
     * @return void
     */
    public function district_sz()
    {
        $type = $this->request->param('type', District::TYPE_SHOP);
        $row = District::getArea(28141, $type);
        foreach($row as &$item){
            if($type == District::TYPE_SHOP){
                $item['name'] = $item['shop_name'];
            }
            unset($item['shop_name']);
        }
        $this->success('返回成功', $row);
    }
}
